<template>
  <el-dialog
    :title="!dataForm.purchaseId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="小区" prop="plot">
      <el-input v-model="dataForm.plot" placeholder="小区"></el-input>
    </el-form-item>
    <el-form-item label="房产类型" prop="houseType">
      <el-input v-model="dataForm.houseType" placeholder="房产类型"></el-input>
    </el-form-item>
    <el-form-item label="区域id" prop="cityId">
      <el-input v-model="dataForm.cityId" placeholder="区域id"></el-input>
    </el-form-item>
    <el-form-item label="面积" prop="area">
      <el-input v-model="dataForm.area" placeholder="面积"></el-input>
    </el-form-item>
    <el-form-item label="求购预算" prop="buyAbout">
      <el-input v-model="dataForm.buyAbout" placeholder="求购预算"></el-input>
    </el-form-item>
    <el-form-item label="标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="content">
      <el-input v-model="dataForm.content" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="用户名id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户名id"></el-input>
    </el-form-item>
    <el-form-item label="用户名名称" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名名称"></el-input>
    </el-form-item>
    <el-form-item label="房源id" prop="typeId">
      <el-input v-model="dataForm.typeId" placeholder="房源id"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          purchaseId: 0,
          plot: '',
          houseType: '',
          cityId: '',
          area: '',
          buyAbout: '',
          title: '',
          content: '',
          userId: '',
          userName: '',
          typeId: '',
          createTime: ''
        },
        dataRule: {
          plot: [
            { required: true, message: '小区不能为空', trigger: 'blur' }
          ],
          houseType: [
            { required: true, message: '房产类型不能为空', trigger: 'blur' }
          ],
          cityId: [
            { required: true, message: '区域id不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '面积不能为空', trigger: 'blur' }
          ],
          buyAbout: [
            { required: true, message: '求购预算不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户名id不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '用户名名称不能为空', trigger: 'blur' }
          ],
          typeId: [
            { required: true, message: '房源id不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.purchaseId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.purchaseId) {
            this.$http({
              url: this.$http.adornUrl(`/house/houserent/info/${this.dataForm.purchaseId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.plot = data.houseRent.plot
                this.dataForm.houseType = data.houseRent.houseType
                this.dataForm.cityId = data.houseRent.cityId
                this.dataForm.area = data.houseRent.area
                this.dataForm.buyAbout = data.houseRent.buyAbout
                this.dataForm.title = data.houseRent.title
                this.dataForm.content = data.houseRent.content
                this.dataForm.userId = data.houseRent.userId
                this.dataForm.userName = data.houseRent.userName
                this.dataForm.typeId = data.houseRent.typeId
                this.dataForm.createTime = data.houseRent.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/houserent/${!this.dataForm.purchaseId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'purchaseId': this.dataForm.purchaseId || undefined,
                'plot': this.dataForm.plot,
                'houseType': this.dataForm.houseType,
                'cityId': this.dataForm.cityId,
                'area': this.dataForm.area,
                'buyAbout': this.dataForm.buyAbout,
                'title': this.dataForm.title,
                'content': this.dataForm.content,
                'userId': this.dataForm.userId,
                'userName': this.dataForm.userName,
                'typeId': this.dataForm.typeId,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
